package com.fadadianqi.search;

import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.model.Village;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.logistics.search.GoodsIndexService;
import com.fadadianqi.logistics.search.SellIndexService;
import com.fadadianqi.logistics.search.VillageIndexService;
import com.fadadianqi.logistics.sell.model.Bill;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class IndexJob {

    private static final Log log = LogFactory.getLog(IndexJob.class);

    private static Boolean IS_RUNNING = Boolean.FALSE;

    @Resource(name = "customerIndexService")
    private AbstractIndexService customerIndexService;

    @Resource(name = "sellIndexService")
    private SellIndexService sellIndexService;

    @Resource(name = "villageIndexService")
    private VillageIndexService villageIndexService;

    @Resource(name = "goodsIndexService")
    private GoodsIndexService goodsIndexService;

    public synchronized void rebuild() throws Exception {
        customerIndexService.rebuild();
        sellIndexService.rebuild();
        villageIndexService.rebuild();
    }


    public synchronized void run() {
        if (IS_RUNNING) {
            log.warn("index is IS_RUNNING...");
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("#run# customer index is running? " + customerIndexService.isRunning());
            log.debug("#run# sell index is running? " + sellIndexService.isRunning());
            log.debug("#run# village index is running? " + villageIndexService.isRunning());
        }
        IS_RUNNING = Boolean.TRUE;
        Map<String, Object> buffer = IndexObjectBufferPool.getBuffer();
        Set<String> sets = buffer.keySet();
        List<String> keys = new ArrayList<String>();
        for (String key : sets) {
            Object object = buffer.get(key);
            if (log.isDebugEnabled()) {
                log.debug("index: " + object.getClass().getName());
            }
            index(object);
            keys.add(key);
        }
        for (String key : keys) {
            buffer.remove(key);
        }
        IS_RUNNING = Boolean.FALSE;
    }

    private boolean index(Object object) {
        try {
            if (object instanceof Customer) {
                Customer p = (Customer) object;
                customerIndexService.removeIndex(p.getId());
                customerIndexService.build(object);
            } else if (object instanceof Bill) {
                Bill p = (Bill) object;
                sellIndexService.removeIndex(p.getId());
                sellIndexService.build(object);
            } else if (object instanceof Village) {
                Village p = (Village) object;
                villageIndexService.removeIndex(p.getId());
                villageIndexService.build(object);
            } else if (object instanceof Goods) {
                Goods p = (Goods) object;
                goodsIndexService.removeIndex(p.getId());
                goodsIndexService.build(object);
            }
            return Boolean.TRUE;
        } catch (Exception e) {
            log.error(e);

        }
        return Boolean.FALSE;
    }
}
